﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;

namespace SpaceArcade
{
    class RatingTable : Scene
    {
        public RatingTable(Background bg, Form f)
            :base(bg, f)
        { }
       
        List<int> scores;
        List<string> nickNames;

        public RatingTable(Form f)
        {
            this.Background = new Background("black.png", 800, 600);
            this.GameForm = f;
            this.gameBM = new Bitmap(GameForm.Width, GameForm.Height);
            

            scores = new List<int>();
            nickNames = new List<string>();

            string tempstr;

            System.IO.FileInfo fl = new System.IO.FileInfo("records.txt");
            if (fl.Exists)
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader("records.txt"))
                {
                    while ((tempstr = sr.ReadLine()) != null)
                    {
                        int temp = Convert.ToInt32(tempstr.Substring(tempstr.IndexOf(':') + 1));
                        string nameStr = tempstr.Substring(0, (tempstr.IndexOf(':')));                        
                        scores.Add(temp);
                        nickNames.Add(nameStr);                                                
                    }
                }
            }
            this.Sort();            
        }

        private void Sort()
        {
            for (int i = 0; i < scores.Count - 1; i++)
            {
                for (int j = i + 1; j < scores.Count; j++)
                {
                    if (scores[i] < scores[j])
                    {
                        int temp = scores[i];
                        scores[i] = scores[j];
                        scores[j] = temp;
                        string _temp = nickNames[i];
                        nickNames[i] = nickNames[j];
                        nickNames[j] = _temp;
                    }
                }
            }  
        }       

        public override void Draw()
        {
            base.Draw();
            Graphics.FromImage(gameBM).DrawString("№", new Font("Cooper black", 34), Brushes.White, 10, 10);
            Graphics.FromImage(gameBM).DrawString("Nickname", new Font("Cooper black", 34),Brushes.White, 100, 10);
            Graphics.FromImage(gameBM).DrawString("Score", new Font("Cooper black", 34), Brushes.White, 500, 10);
            for (int i = 0; i < 10; i++)
            {
                Graphics.FromImage(gameBM).DrawString(Convert.ToString(i + 1), new Font("Cooper black", 36), Brushes.White, 10, 50 + i * 40);
                Graphics.FromImage(gameBM).DrawString(nickNames[i], new Font("Cooper black", 36), Brushes.White, 100, 50 + i * 40);
                Graphics.FromImage(gameBM).DrawString(Convert.ToString(scores[i]), new Font("Cooper black", 36), Brushes.White, 500, 50 + i * 40);
            }
        }

    }
}
